#pragma once
#include <string>

#include "FieldDataType.h"
// TYPE 只能是char[] 或者 string

template<typename TYPE>
inline void AssignBuffer(TYPE& dest, const char* str, int len){
	memset(dest, 0, sizeof(TYPE));
	if (str && len > 0)
	{
		len = len >= sizeof(TYPE) ? (sizeof(TYPE) - 1) : len;
		if (len>0)
			memcpy(dest, str, len);
		dest[len] = 0;
	}
}
template<> inline void AssignBuffer<std::string>(std::string& dest, const char* str, int len)
{
	if (len <= 0 || str==0)dest.clear();
	else dest = std::string(str?str:"", (str && len>0)?len:0);
}

template<typename TYPE>
inline const char* GetBufferPtr(const TYPE& buf){return buf;}
template<> inline const char* GetBufferPtr<std::string>(const std::string& buf)
{
	return buf.c_str();
}

// 默认类型当成char[]数组,其他类型,需要特例化
template<typename TYPE>
bool IsTypeDataEqual(const TYPE& d1, const TYPE& d2){
	return strcmp(d1, d2) == 0;
}

/*
	返回的type,高16位表示kFieldDataType,低16位表示数组长度,单个则为1
*/
template<typename TYPE>
inline int GetTypeEncoding(){ return (kFieldDataTypeChar << 16) + sizeof(TYPE); }

template<>  inline int GetTypeEncoding<double>(){ return (kFieldDataTypeDouble << 16) + 0; }
template<>  inline int GetTypeEncoding<float>(){ return (kFieldDataTypeFloat << 16) + 0; }
template<>  inline int GetTypeEncoding<int>(){ return (kFieldDataTypeInt << 16) + 0; }
template<>  inline int GetTypeEncoding<uint32_t>(){ return (kFieldDataTypeUINT << 16) + 0; }
template<>  inline int GetTypeEncoding<short>(){ return (kFieldDataTypeShort << 16) + 0; }
template<>  inline int GetTypeEncoding<unsigned short>(){ return (kFieldDataTypeUShort << 16) + 0; }
template<>  inline int GetTypeEncoding<bool>(){ return (kFieldDataTypeBool << 16) + 0; }
template<>  inline int GetTypeEncoding<char>(){ return (kFieldDataTypeChar << 16) + 0; }
template<>  inline int GetTypeEncoding<std::string>(){ return (kFieldDataTypeString << 16) + 0; }
